home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_10 / 9n10042b < prev    next >
Text File  |  1991-07-07  |  1KB  |  49 lines

  1. /* These enhanced "wrappers" pad the dynamic object
  2.    with a serial number.
  3.  
  4.    The adjptr manipulations are a kludge. I can't do
  5.    the necessary pointer arithmetic on void pointers,
  6.    so I create char pointers, manipulate them and
  7.    then cast back. There must be a better way!
  8. */   
  9.  
  10. #ifndef HWRAPPER
  11. #ifdef DEBUG
  12. #define HWRAPPER
  13. int count;
  14. int fcount;   /* count free calls separately */
  15. FILE * log;
  16.  
  17. void my_free(void * tree)
  18. {
  19.         char * adjptr;
  20.         
  21.         adjptr = (char *) tree;
  22.     adjptr -= sizeof(int) ; 
  23.     if (log == NULL) log = fopen("log","w");
  24.     fprintf(log,"%p %5.5d free %d\n",adjptr, * ((int * ) adjptr),fcount++ );
  25.         free( (void *) adjptr );
  26. }
  27.  
  28. void * my_alloc(size_t size)
  29. {
  30.     void * temp;
  31.         char * adjptr;
  32.     
  33.     if (log == NULL) log = fopen("log","w");
  34.     temp = (void *) malloc( size + sizeof(int) );
  35.         *( (int * ) temp ) = count;
  36.     fprintf(log,"%p %5.5d anew\n",temp,count++);
  37.         adjptr = (char *) temp;
  38.         adjptr += sizeof(int);
  39.     return (void *) adjptr ;
  40. }
  41.  
  42. #define malloc(x) my_alloc(x)
  43.  
  44. #define free(x)  my_free(x)
  45.  
  46. #endif
  47. #endif
  48.  
  49.